<?php
namespace haibao\cms\business;

use \haibao\cms\data\Interactive as InteractiveData;
use \haibao\cms\model\filter\InteractiveResource as InteractiveFilter;
use haibao\frame\data\query\Condition;
use \haibao\cms\model\data\InteractiveResource as InteractiveModel;
use haibao\app\business\BusinessException;
use haibao\cms\data\BaseMysql;


class Interactive extends \haibao\cms\business\Base {
	const STATUS_CONTENT_EMPTY = 0;
	const STATUS_CONTENT_EXIST = 1;
	const STATUS_SENDED = 2;
	
	public static function checkSendable($id){
		if (empty($id)){
			throw new BusinessException('Id不能为空');
		}
		
		$status = self::STATUS_CONTENT_EMPTY;
		$filter = new \haibao\cms\model\filter\InteractiveResource();
		$filter->where($filter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$id);
		$data = self::searchInteractive($filter);
		if ($data){
			$data = array_pop($data);
			if ($data->Status == \haibao\cms\model\data\InteractiveResource::STATUS_SEND){
				$status = self::STATUS_SENDED;
			} else if ($data->ContentLink){
				$status = self::STATUS_CONTENT_EXIST;
			}
		}
		
		return $status;
	}
	
	public static function searchInteractive($filter){
		$data = new InteractiveData();
		 
		return $data->getAll($filter);
	}
	
	public static function del($id){
		self::checkOperation(self::FUNC_CODE_INTERACTIVE_RESOURCE_DELETE);
		if (empty($id)){
			throw new BusinessException('Id不能为空');
		}
		
		$data = new InteractiveData();
		$filter = new InteractiveFilter();
		$filter->where(InteractiveFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$id);
		$interactive = $data->getOne($filter);
		if ($interactive){
			if ($interactive->Status == \haibao\cms\model\data\InteractiveResource::STATUS_SEND){
				throw new BusinessException('已推送的资源不能删除');
			}
		}
		
		$data->del($id);
		$operationData = array(
				'id'=>$id,
				'resourceDate'=>$interactive->ResourceDate->format('Y-m-d H:i:s'),
				'brandName'=>$interactive->BrandName,
				'description'=>$interactive->Describe
		);
		self::saveOperateHistory(self::FUNC_CODE_INTERACTIVE_RESOURCE_DELETE, $operationData);
	}
	
// 	public static function getOneInteractiveHistory($id){
// 	    $data = new InteractiveData();
// 	    return $data->getOneInteractiveHistory($id);
// 	}
	
	public static function getOneInteractiveHistory($query){
	    $data = new InteractiveData();
	    return $data->getAll($query);
	}
	
	public static function editContentLink($id,$contentLink){
		self::checkOperation(self::FUNC_CODE_INTERACTIVE_RESOURCE_EDIT);
		if (empty($id)){
			throw new BusinessException('Id不能为空');
		}
		
		$data = new InteractiveData();
		$data->editContentLink($id,$contentLink);
		self::saveOperateHistory(self::FUNC_CODE_INTERACTIVE_RESOURCE_EDIT, array('Id'=>$id,'ContentLink'=>$contentLink));
	}
	
	public static function sendInteractive($id,$contentLink = ''){
		self::checkOperation(self::FUNC_CODE_INTERACTIVE_RESOURCE_EDIT);
		if (empty($id)){
			throw new BusinessException('Id不能为空');
		}
		
		$data = new InteractiveData();
		$filter = new InteractiveFilter();
		$filter->where(InteractiveFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$id);
		$filter->select(array(
				InteractiveFilter::CONDITION_FIELD_NAME_CONTENT_LINK
		));
		$result = $data->getAll($filter);
		if ($result && empty($result[0]->ContentLink) && !$contentLink){
			throw new BusinessException('needContentLink');
		}
		
		$data->sendInteractive($id,$contentLink);
		self::saveOperateHistory(self::FUNC_CODE_INTERACTIVE_RESOURCE_EDIT, array('Id'=>$id,'Content'=>'设置推送'));
	}
	
	public static function addInteractive($model){
		self::checkOperation(self::FUNC_CODE_INTERACTIVE_RESOURCE_ADD);
		self::checkModel($model);
		self::checkCountLimit($model);
		
		$model->CreateTime = new \DateTime();
		$model->CreateBy = self::getCurrentUser()->Id;
		
		$data = new InteractiveData();
		$data->add($model);
		
		self::saveOperateHistory(self::FUNC_CODE_INTERACTIVE_RESOURCE_ADD, $model->toArray(true,true));
	}
	
	public static function updateInteractive($model){
		self::checkOperation(self::FUNC_CODE_INTERACTIVE_RESOURCE_EDIT);
		self::checkModel($model);
		$data = new InteractiveData();
		
		$originalModel = $data->getOneById($model->Id);
		$originalArr = array();
		foreach($originalModel as $key=>$val){
		    $originalArr['ResourceType'] = $originalModel->ResourceType;
		    $originalArr['ResourceDate'] = $originalModel->ResourceDate->format('Y-m-d');
		    $originalArr['BrandName'] = $originalModel->BrandName;
		    $originalArr['Describe'] = $originalModel->Describe;
		    $originalArr['Director'] = $originalModel->Director;
		    $originalArr['CreateTime'] = $originalModel->CreateTime->format('Y-m-d H:i:s');;
		    $originalArr['Status'] = $originalModel->Status;
		    $originalArr['ContentLink'] = $originalModel->ContentLink;
		}
		$arrSet = array(
				InteractiveFilter::CONDITION_FIELD_NAME_RESOURCE_TYPE => $model->ResourceType,
				InteractiveFilter::CONDITION_FIELD_NAME_RESOURCE_DATE => $model->ResourceDate,
				InteractiveFilter::CONDITION_FIELD_NAME_BRAND_NAME => $model->BrandName,
				InteractiveFilter::CONDITION_FIELD_NAME_DESCRIBE => $model->Describe,
				InteractiveFilter::CONDITION_FIELD_NAME_DIRECTOR => $model->Director,
				InteractiveFilter::CONDITION_FIELD_NAME_STATUS => $model->Status,
				InteractiveFilter::CONDITION_FIELD_NAME_CONTENT_LINK => $model->ContentLink,
		);
		$data->updateById($model->Id, $arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_INTERACTIVE_RESOURCE_EDIT, array_merge($arrSet,array('Id'=>$model->Id)));
		
		$historyModel = new \haibao\cms\model\data\InteractiveResourceHistory();
		$historyModel->InteractiveId = $model->Id;
		$historyModel->UserId = self::getCurrentUser()->Id;
		$historyModel->CreateTime = date('Y-m-d H:i:s');
		$historyModel->OperationContent = json_encode($originalArr);
		$data->add($historyModel);
	}
	
	public static function searchCount($resourceType,$startDate,$endDate){
		$data = new InteractiveData();
		
		return $data->searchCount($resourceType,$startDate,$endDate);
	}
	
	private static function checkModel($model){
		if (empty($model->ResourceType)){
			throw new BusinessException('资源类型不能为空');
		}
		if (empty($model->ResourceDate)){
			throw new BusinessException('发布日期不能为空');
		}
		if (empty($model->BrandName)){
			throw new BusinessException('品牌名称不能为空');
		}
		if (empty($model->Director)){
			throw new BusinessException('对接人不能为空');
		}
	}
	
	private static function checkCountLimit($model){
		if ($model->ResourceType != InteractiveModel::TYPE_BBS && $model->ResourceType != InteractiveModel::TYPE_VIDEO){
			$limit = 10;
			switch ($model->ResourceType){
				case InteractiveModel::TYPE_FWMA:
					$limit = InteractiveModel::COUNT_LIMIT_FWMA;
					break;
				case InteractiveModel::TYPE_WEIBO:
					$limit = InteractiveModel::COUNT_LIMIT_WEIBO;
					break;
				case InteractiveModel::TYPE_WEIXIN:
					$limit = InteractiveModel::COUNT_LIMIT_WEIXIN;
					break;
			}
			
			$data = new InteractiveData();
			$count = $data->getInteractiveCount($model->ResourceType,$model->ResourceDate);
			
			if ($count >= $limit){
				throw new BusinessException('该类型的资源一天内限制添加'.$limit.'条');
			}
		}
	}
}